
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>安装和初始化 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="1.install.html">
            
                <a href="1.install.html">
            
                    
                    安装和初始化
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >安装和初始化</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5B89;&#x88C5;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;-webpack">&#x5B89;&#x88C5;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A; webpack</h1>
<h2 id="1-&#x521D;&#x59CB;&#x5316;-node-&#x73AF;&#x5883;">1. &#x521D;&#x59CB;&#x5316; <code>node</code> &#x73AF;&#x5883;</h2>
<p>&#x5047;&#x5B9A;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x597D; <code>nodejs</code> &#x548C; <code>npm</code> &#x3002;<br>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#x5E76;&#x521D;&#x59CB;&#x4E00;&#x4E2A;<code>node</code> &#x73AF;&#x5883;  </p>
<pre><code class="lang-sh">mkdir webpack_project
<span class="hljs-built_in">cd</span> webpack_project
npm init
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x7528; <code>yarn</code> &#x6765;&#x521D;&#x59CB;&#x5316;&#x3002;</p>
<pre><code class="lang-sh"><span class="hljs-comment"># &#x6CA1;&#x6709;&#x5B89;&#x88C5;yarn&#xFF0C;&#x5148;&#x5B89;&#x88C5;&#x5B83;</span>
sudo npm install -g yarn
<span class="hljs-comment"># &#x6709;&#x4E86;yarn &#x540E;, &#x53EF;&#x4EE5;&#x7528;&#x5B83;&#x6765;&#x521D;&#x59CB;&#x5316;</span>
yarn init
</code></pre>
<blockquote>
<p>package name: (webpack_project)<br>version: (1.0.0)<br>description:<br>entry point: (index.js)<br>test command:<br>git repository:<br>keywords:<br>author: uomer<br>license: (ISC)<br>About to write to /home/gscm/webpack_project/package.json:  </p>
<p>{<br>&quot;name&quot;: &quot;webpack_project&quot;,<br>&quot;version&quot;: &quot;1.0.0&quot;,<br>&quot;description&quot;: &quot;&quot;,<br>&quot;main&quot;: &quot;index.js&quot;,<br>&quot;scripts&quot;: {<br> &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>},<br>&quot;author&quot;: &quot;uomer&quot;,<br>&quot;license&quot;: &quot;ISC&quot;<br>}  </p>
<p>Is this OK? (yes)  </p>
</blockquote>
<h2 id="2-&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x6DFB;&#x52A0;-webpack-webpack-cli">2. &#x5728;&#x9879;&#x76EE;&#x4E2D;&#x6DFB;&#x52A0; <code>webpack</code> <code>webpack-cli</code></h2>
<p>&#x63A8;&#x8350;&#x4F7F;&#x7528; <code>yarn</code> &#x6765;&#x5B89;&#x88C5; <code>node</code> &#x6A21;&#x5757;<br>&#x5728;linux&#x7EC8;&#x7AEF;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x547D;&#x4EE4;&#xFF1A;  </p>
<pre><code class="lang-sh"><span class="hljs-comment"># -D &#x4E5F;&#x53EF;&#x4EE5;&#x662F; --dev &#x662F;&#x4EE5;&#x5F00;&#x53D1;&#x6A21;&#x5F0F;&#x6DFB;&#x52A0;</span>
yarn add -D webpack webpack-cli
</code></pre>
<h2 id="3-&#x521D;&#x59CB;&#x76EE;&#x5F55;&#x7ED3;&#x6784;">3. &#x521D;&#x59CB;&#x76EE;&#x5F55;&#x7ED3;&#x6784;</h2>
<h3 id="&#x521B;&#x5EFA;-src-config-&#x76EE;&#x5F55;--src-&#x653E;&#x7A0B;&#x5E8F;&#x6E90;&#x6587;&#x4EF6;-config-&#x653E;&#x4E00;&#x4E9B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;-&#x7136;&#x540E;&#x914D;&#x7F6E;-webpackconfigjs">&#x521B;&#x5EFA; <code>src</code> <code>config</code> &#x76EE;&#x5F55;,  src &#x653E;&#x7A0B;&#x5E8F;&#x6E90;&#x6587;&#x4EF6;, config &#x653E;&#x4E00;&#x4E9B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;, &#x7136;&#x540E;&#x914D;&#x7F6E; webpack.config.js</h3>
<pre><code class="lang-sh">mkdir src config
vim config/webpack.config.js
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">// config/webpack.config.js</span>
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>)
<span class="hljs-built_in">module</span>.exports = {
  entry: <span class="hljs-string">&apos;src/index.js&apos;</span>
  output: {
    path: path.resolve(__dirname, <span class="hljs-string">&apos;dist&apos;</span>),
    filename: <span class="hljs-string">&apos;[name].[chunkHash:8].js&apos;</span>
  }
}
</code></pre>
<h3 id="packagejson-&#x4E2D;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF0C;&#x542F;&#x52A8;webpack">package.json &#x4E2D;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF0C;&#x542F;&#x52A8;webpack</h3>
<pre><code class="lang-json"><span class="hljs-string">&quot;scripts&quot;</span>: {
  <span class="hljs-string">&quot;build&quot;</span>: <span class="hljs-string">&quot;webpack --mode development --config ./config/webpack.config.js&quot;</span>
}
</code></pre>
<h3 id="&#x6DFB;&#x52A0;html-webpack-plugin">&#x6DFB;&#x52A0;html-webpack-plugin</h3>
<pre><code class="lang-sh">yarn add -D html-webpack-plugin
</code></pre>
<h3 id="&#x914D;&#x7F6E;-html-webpack-plugin">&#x914D;&#x7F6E; html-webpack-plugin</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5728; webpack.config.js&#x6587;&#x4EF6;&#x4E2D;&#x63D2;&#x5165;</span>
<span class="hljs-keyword">const</span> HtmlWebpackPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;html-webpack-plugin&apos;</span>)
<span class="hljs-comment">// &#x5728; module.exports &#x91CC;&#x9762;&#x63D2;&#x5165;</span>
plugins: [
  <span class="hljs-keyword">new</span> HtmlWebpackPlugin({
    title: <span class="hljs-string">&apos;document_title&apos;</span>,
    template: <span class="hljs-string">&apos;public/index&apos;</span> <span class="hljs-comment">// index.html &#x8DEF;&#x5F84;</span>
  })
]
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 介绍">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"安装和初始化","level":"1.2","depth":1,"previous":{"title":"介绍","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"1.install.md","mtime":"2020-04-16T09:15:25.905Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-16T07:59:18.233Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

